package com.cj.wisdom_display.utils;

import android.media.MediaPlayer;
import android.os.Handler;

import com.cj.wisdom_display.MyApplication;
import com.yc.videocache.cache.ProxyVideoCacheManager;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Android 音频播放 封装
 *
 * @author zengyi
 * create at 2020/10/15 13:32
 */
public class MusicPlayer {
	private MediaPlayer mPlayer;
	public boolean isInitialized = false;//是否已初始化
	private Timer mTimer;
	private Handler mHandler = null;

	public MusicPlayer(String url) {
		if (NetUtils.isNetworkAvailable(MyApplication.instance)) {
			//构造函数中
			String proxyUrl = ProxyVideoCacheManager.getProxy(MyApplication.instance).getProxyUrl(url);
			mTimer = new Timer();
			mHandler = new Handler();
			Thread initThread = new Thread(() -> init(proxyUrl));
			initThread.start();
			//开始方法中
			mTimer.schedule(new TimerTask() {
				@Override
				public void run() {
					if (isPlaying()) {
						int pos = mPlayer.getCurrentPosition();
						int duration = mPlayer.getDuration();
						mHandler.post(() -> {
							if (mOnSeekListener != null) {
								mOnSeekListener.OnSeek((int) (pos * 1.f / duration * 100));
							}
						});
					}
				}
			}, 0, 1000);
		} else {
			UtilsKt.toastStr(MyApplication.instance, "设备没有网络");
		}
	}

	private void init(String url) {
		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(url);
			mPlayer.prepareAsync();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mPlayer.setOnErrorListener((mp, what, extra) -> {
			//处理错误 返回false会触发onCompletion
			return true;
		});

		//播放完成监听
		mPlayer.setOnCompletionListener(mp -> {
			if (mOnSeekListener != null) {
				mOnSeekListener.onComplete();
			}
		});

		//当装载流媒体完毕的时候回调
		mPlayer.setOnPreparedListener(mp -> {
			isInitialized = true;
			if (mOnSeekListener != null) {
				mOnSeekListener.loadComplete();
			}
		});

	}

	/**
	 * 播放
	 */
	public void start() {
		if (mPlayer == null || !isInitialized || isPlaying()) return;
		//未初始化和正在播放时return
		mPlayer.start();
	}

	/**
	 * 是否正在播放
	 */
	public boolean isPlaying() {
		//未初始化和正在播放时return
		try {
			if (!isInitialized || mPlayer == null) {
				return false;
			}
			return mPlayer.isPlaying();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 销毁播放器
	 */
	public void onDestroyed() {
		if (mPlayer != null) {
			mPlayer.stop();
			mPlayer.release();//释放资源
			mPlayer = null;
		}
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		isInitialized = false;
		if (mHandler != null) {
			mHandler.removeCallbacksAndMessages(null);
		}
	}

	/**
	 * 停止播放器
	 */
	public void stop() {
		if (mPlayer != null && isPlaying()) {
			mPlayer.stop();
		}
	}

	/**
	 * 暂停播放器
	 */
	public void pause() {
		if (mPlayer != null && isPlaying()) {
			mPlayer.pause();
		}
	}


	/**
	 * 跳转到
	 *
	 * @param pre_100 0~100
	 */
	public void seekTo(int pre_100) {
		if (mPlayer != null && isInitialized) {
			pause();
			mPlayer.seekTo((int) (pre_100 / 100.f * mPlayer.getDuration()));
			start();
		}
	}


	//------------设置进度监听-----------
	public interface OnSeekListener {
		void OnSeek(int per_100);

		void onComplete();

		void loadComplete();
		
	}

	private OnSeekListener mOnSeekListener;

	public void setOnSeekListener(OnSeekListener onSeekListener) {
		mOnSeekListener = onSeekListener;
	}

}
